package cn.ulyer.common.jpa.entity;

import cn.ulyer.common.constants.AuthStrategyEnum;
import cn.ulyer.common.model.AbstractBaseModel;
import lombok.Getter;
import lombok.Setter;
import org.springframework.security.core.GrantedAuthority;

import javax.persistence.*;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

@Getter
@Setter
@Entity
@Table(name = "t_permission",indexes = {@Index(name = "pid_idx",columnList = "parentId")
        ,@Index(name = "type_index",columnList = "permissionType")})
public class Permission extends AbstractBaseModel implements GrantedAuthority {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    @Column(columnDefinition = "int(10) comment '父级id' default 0", nullable = false)
    private Integer parentId;

    @Column(columnDefinition = "varchar(32) comment '为菜单时的icon'")
    private String icon;

    @Column(columnDefinition = "varchar(64) comment '为菜单时的组件路径'")
    private String component;

    @NotBlank
    @Column(columnDefinition = "varchar(255) comment '资源名称'", nullable = false)
    private String name;

    @Column(columnDefinition = "varchar(64) comment '权限值'", nullable = false)
    private String permValue;

    @Column(columnDefinition = "varchar(64) comment '前端路由或后端rest'", nullable = false)
    private String url;

    @Column(columnDefinition = "tinyint(1) comment '状态' default 1", insertable = false, nullable = false)
    private Integer status;

    @Column(columnDefinition = "int comment '优先级' ")
    private Integer priority;

    @Enumerated(EnumType.STRING)
    @NotNull
    private PermissionType permissionType = PermissionType.MENU;

    @Column(columnDefinition = "varchar(64) comment '权限校验逻辑 如NO_AUTH  PERMISSION'", nullable = false)
    private String authStrategy = AuthStrategyEnum.AUTHENTICATED.name();

    @Override
    public String getAuthority() {
        return permValue;
    }


    public enum PermissionType {
        MENU,
        API,
        MODULE
    }
}
